package keeto.com.vn.bms.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class BMSDatabase {
	private SQLiteDatabase mDB;
	private SQLiteDatabase mReadDB;
	private static BMSDatabase database;
	
	public BMSDatabase(BMSDBHelper helper) throws SQLException{
		if (mDB == null){
			mDB = helper.getWritableDatabase();
		}
		mReadDB = helper.getReadableDatabase();
	}
	public static BMSDatabase getInstance(BMSDBHelper helper){
		database = new BMSDatabase(helper);
		return database;
	}
	
	public SQLiteDatabase getmDB() {
		return mDB;
	}

	public void setmDB(SQLiteDatabase mDB) {
		this.mDB = mDB;
	}

	public SQLiteDatabase getmReadDB() {
		return mReadDB;
	}

	public void setmReadDB(SQLiteDatabase mReadDB) {
		this.mReadDB = mReadDB;
	}

	public void close(){
		mDB.close();
		mReadDB.close();
	}
	public Cursor rawQuery(String sql, String[] selectionArgs) {
		return mReadDB.rawQuery(sql, selectionArgs);
	}

	public Cursor query(String table, String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy) {
		return mReadDB.query(table, columns, selection, selectionArgs, groupBy,
				having, orderBy);
	}

	public Cursor query(String table, String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy, String limit) {
		return mReadDB.query(table, columns, selection, selectionArgs, groupBy,
				having, orderBy, limit);
	}

	public Cursor query(boolean distinct, String table, String[] columns,
			String selection, String[] selectionArgs, String groupBy,
			String having, String orderBy, String limit) {
		return mReadDB.query(distinct, table, columns, selection, selectionArgs,
				groupBy, having, orderBy, limit);
	}

	public long insert(String table, String nullColumnHack, ContentValues values) {
		return getmDB().insert(table, nullColumnHack, values);

	}

	public int update(String table, ContentValues values, String whereClause,
			String[] whereArgs) {
		return getmDB().update(table, values, whereClause, whereArgs);

	}

	public void execSQL(String sql) {
		getmDB().execSQL(sql);
	}

	public int delete(String table, String whereClause, String[] whereArgs) {
		return getmDB().delete(table, whereClause, whereArgs);
	}
}
